package com.smart.auth.controller;



import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;


import java.security.Principal;
import java.util.Map;

/**
 * 认证中心
 *
 * @author yangbingquan
 */
@RestController
@RequestMapping("/oauth")
@AllArgsConstructor
@Slf4j
public class AuthController {

    private TokenEndpoint tokenEndpoint;


    @GetMapping
    public String test() {
        return "test";
    }

    /*
        @ApiOperation(value = "OAuth2认证", notes = "login")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "grant_type", defaultValue = "password", value = "授权模式", required = true),
                @ApiImplicitParam(name = "client_id", defaultValue = "client", value = "Oauth2客户端ID", required = true),
                @ApiImplicitParam(name = "client_secret", defaultValue = "123456", value = "Oauth2客户端秘钥", required = true),
                @ApiImplicitParam(name = "refresh_token", value = "刷新token"),
                @ApiImplicitParam(name = "username", defaultValue = "admin", value = "登录用户名"),
                @ApiImplicitParam(name = "password", defaultValue = "123456", value = "登录密码")
        })*/
    @PostMapping("/token")
    public OAuth2AccessToken postAccessToken(Principal principal, @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        OAuth2AccessToken oAuth2AccessToken;
        parameters.put("grant_type", "password");
        parameters.put("client_id", "client");
        parameters.put("client_secret", "123456");

        /**
         * 获取登录认证的客户端ID
         *
         * 兼容两种方式获取Oauth2客户端信息（client_id、client_secret）
         * 方式一：client_id、client_secret放在请求路径中
         * 方式二：放在请求头（Request Headers）中的Authorization字段，且经过加密，例如 Basic Y2xpZW50OnNlY3JldA== 明文等于 client:secret
         */
        try {
            oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
            return oAuth2AccessToken;
        } catch (HttpRequestMethodNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }

    private PasswordEncoder passwordEncoder;


}
